﻿package UserInterface;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.*;

import Chapter06.SimpleBomb;

public class MineSweepGame implements MouseListener {
	int width;
	int height;
	int numOfBombs;
	
	SimpleBomb bombGame;
	ArrayList<JButton> buttons;
	HashMap<Integer, Color> colorTable;
	// TODO 宣告一個存放內容為0的格子序號ArrayList: zeroIndexArray
	ArrayList<Integer> readyToOpenIndexList;
	ArrayList<Boolean> flagCells;

	boolean isAnimation = false;
	
	JCheckBox checkbox;
	
	public void run(int w, int h, int b) {
		width = w;
		height = h;
		numOfBombs = b;
		
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		JPanel panelMenu = new JPanel();
		JLabel lblWidth = new JLabel("寬");
		JTextField textWidth = new JTextField(6);
		JLabel lblHeight = new JLabel("高");
		JTextField textHeight = new JTextField(6);
		checkbox = new JCheckBox("動畫演出");
		panelMenu.add(lblWidth);
		panelMenu.add(textWidth);
		panelMenu.add(lblHeight);
		panelMenu.add(textHeight);
		panelMenu.add(checkbox);
		
		buttons = new ArrayList<JButton>();
		readyToOpenIndexList = new ArrayList<Integer>();
		
		colorTable = new HashMap<Integer, Color>();
		colorTable.put(0, Color.BLACK);
		colorTable.put(1, Color.BLUE);
		colorTable.put(2, Color.GREEN);
		colorTable.put(3, Color.RED);
		colorTable.put(4, Color.CYAN);
		colorTable.put(5, Color.ORANGE);
		colorTable.put(6, Color.PINK);
		colorTable.put(7, Color.MAGENTA);
		colorTable.put(8, Color.GRAY);
		
		bombGame = new SimpleBomb();
		bombGame.setupTable(w, h, b);
		
		// Color(red, blue, green)
		panel.setBackground(Color.LIGHT_GRAY);
		
		// 更改 panel 的 layout 預設為 FlowLayout
		panel.setLayout(new GridLayout(h, w));
		
		for (int i = 0; i < h*w; ++i) {
			JButton btn = new JButton("");
			panel.add(btn);
			buttons.add(btn);
			btn.setMargin(new Insets(2, 2, 2, 2));
			// 註冊事件通知
			btn.addMouseListener(this);
		}
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		frame.add(panel, BorderLayout.CENTER);
		frame.add(panelMenu, BorderLayout.NORTH);
		Insets i = frame.getInsets();
		frame.setSize(width * 30 + i.left + i.right,
				height * 30 + i.top + i.bottom);
		
		frame.setVisible(true);
	}

	
	public void getIndexesOfZeroes(int index) {
		// TODO
		// 從 buttons 藉由 index 拿出 button 物件
		
		// 重新計算，清空上次計算結果
		readyToOpenIndexList.clear();
		
		// 找出所有符合條件的格子並放在"格子序號ArrayList"
		findNeighborZeroes(index);
		
		// 對 "格子序號ArrayList" 裡所有格子作打開的動作
		//  -> 設定按鈕文字
		//  -> 設定按鈕背景
		if (checkbox.isSelected()) {
			Runnable openJob = new OpenButtonsInList(this);
			Thread myThread = new Thread(openJob);
			myThread.start();
		} else {
			for (Integer indexOfZeroCell : readyToOpenIndexList) {
				JButton b = buttons.get(indexOfZeroCell);
				Integer numOfNearBombs = bombGame.getCell(indexOfZeroCell);
				String buttonText = (numOfNearBombs == 0) ? "" : ""+numOfNearBombs;
				b.setText(buttonText);
				
				b.setBackground(Color.GRAY);
				b.setForeground(colorTable.get(numOfNearBombs));
			}
		}
	}
	
	public void findNeighborZeroes(Integer index) {
		Integer numOfNearBombs = bombGame.getCell(index);
		if (numOfNearBombs != -1) {
			readyToOpenIndexList.add(index);
		}
		
		if (numOfNearBombs == 0) {
			boolean isLeftMost = (index % width == 0);
			boolean isRightMost = (index % width == width - 1);
			boolean isTopMost = (index < width);
			boolean isBottomMost = (index >= width * (height-1));

			Integer indexToCheck;
			for (int i = -1; i <= 1; i++) {	// i: 由上到下
				for (int j = -1; j <= 1; j++) {	// j: 由左到右
					if (i == 0 && j == 0) {
						continue; // 檢查格本身，不需要搜尋
					}
					if (isLeftMost && j == -1) {
						continue; // 最左直排的情況，往左不需要
					}
					if (isRightMost && j == 1) {
						continue; // 最右直排的情況，往右不需要
					}
					if (isTopMost && i == -1) {
						continue; // 最上橫排的情況，往上不需要
					}
					if (isBottomMost && i == 1) {
						continue; // 最下橫排的情況，往下不需要
					}
					indexToCheck = index + i * width + j;
					if (readyToOpenIndexList.contains(indexToCheck)) {
						continue; // 已經包含在清單裡，不需要繼續搜尋
					}
					// 通過上面邊界檢查後，才可以繼續搜尋
					findNeighborZeroes(indexToCheck);
				}
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		JButton b = (JButton)(e.getSource());
		int index = buttons.indexOf(b);
		if (e.getButton() == MouseEvent.BUTTON1) {
			Integer result = bombGame.getCell(index);
			if (result == -1) { // 如果是炸彈
				b.setText("B");
				b.setBackground(new Color(250,0,0));
				b.setForeground(new Color(255,255,255));
				Integer answer = JOptionPane.showConfirmDialog(null, "霹靂星球爆炸了，是否重玩");
				// answer JOptionPane.YES_OPTION, NO, CANCEL, OK, CLOSED
			} else if (result == 0) { // 如果是空格 (周圍都沒有炸彈)
				getIndexesOfZeroes(index);
			} else { // 如果是一般數字 (周圍至少有一個炸彈)
				b.setText(""+result);
				b.setForeground(colorTable.get(result));
				b.setBackground(Color.GRAY);
			}			
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			b.setText("F");
			b.setForeground(Color.BLACK);
			b.setBackground(Color.LIGHT_GRAY);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
